//package org.ricks.robot.business;
//
//import org.ricks.common.lang.Logger;
//import org.ricks.common.utils.CollUtil;
//import org.ricks.common.utils.RandomUtils;
//import org.ricks.common.utils.ThreadUtils;
//import org.ricks.net.transport.kcp.KcpSession;
//import org.ricks.protocol.ByteBuf;
//import org.ricks.robot.Monitor;
//import org.ricks.robot.RobotInfo;
//import org.ricks.robot.RobotManager;
//import org.ricks.robot.RobotRun;
//import org.ricks.robot.action.RobotCookCmdAction;
//import org.ricks.robot.action.RobotHandicraftCmdAction;
//import org.ricks.robot.action.RobotSpinCmdAction;
//import org.ricks.robot.action.wrap.RobotCookActionWrap;
//import org.ricks.robot.action.wrap.RobotHandicraftCmdActionWrap;
//import org.ricks.robot.action.wrap.RobotSpinCmdActionWrap;
//import proto.common.FProto;
//import proto.common.LifeArgs;
//import proto.common.PBLifeType;
//
//import java.util.HashMap;
//import java.util.HashSet;
//import java.util.Map;
//import java.util.Set;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.atomic.AtomicLong;
//
//public class RobotLifeAction {
//
//
//    //烹饪
//    public static final Map<Integer, Map<Integer, Integer>> lifeCookItems = new HashMap<>();
//    //制作
//    public static final Map<Integer, Map<Integer, Integer>> makePropItems = new HashMap<>();
//    //手工艺
//    public static final Map<Integer, Map<Integer, Integer>> handicraftItems = new HashMap<>();
//    //纺织
//    public static final Map<Integer, Map<Integer, Integer>> spinItems = new HashMap<>();
//
//
//    public static RobotCookCmdAction robotCookCmdAction = (session, cookId) -> {
//        int randomTime = RandomUtils.randomInt(1000, 5000);
//        RobotInfo robotInfo = RobotManager.get(session);
//        RobotRun.scheduledExecutorService.schedule(() -> {
//            Monitor.addActionRequest(session, FProto.Action.CookingFood);
//            robotInfo.requestTimeMap.put(FProto.Action.CookingFood, System.currentTimeMillis());
//            LifeArgs lifeArgs = LifeArgs.newBuilder()
//                    .setId(cookId)
//                    .setLifeType(PBLifeType.Cooking)
//                    .build();
//
//            FProto fProto = FProto.newBuilder()
//                    .setKey(FProto.Key.Life)
//                    .setAction(FProto.Action.CookingFood)
//                    .setLifeArgs(lifeArgs)
//                    .build();
//
//            byte[] data = fProto.toByteArray();
//
//            ByteBuf buf = new ByteBuf(1024);
//            buf.writeByte((byte) 0x03);
//            buf.writeBytes(data);
//
//            session.send(buf.toArray());
//            RobotAction.taskTimes.put(session, System.currentTimeMillis());
//            robotInfo.doCooking.add(cookId);
//        } , randomTime, TimeUnit.MILLISECONDS);
//    };
//
//    public static final RobotHandicraftCmdAction handicraftCmdAction = (session, handicraftId) -> {
//        int randomTime = RandomUtils.randomInt(1000, 5000);
//        RobotInfo robotInfo = RobotManager.get(session);
//        RobotRun.scheduledExecutorService.schedule(() -> {
//            Monitor.addActionRequest(session, FProto.Action.Handicraft);
//        robotInfo.requestTimeMap.put(FProto.Action.Handicraft, System.currentTimeMillis());
//        LifeArgs lifeArgs = LifeArgs.newBuilder()
//                .setId(handicraftId)
//                .setLifeType(PBLifeType.Handicraft)
//                .build();
//
//        FProto fProto = FProto.newBuilder()
//                .setKey(FProto.Key.Life)
//                .setAction(FProto.Action.Handicraft)
//                .setLifeArgs(lifeArgs)
//                .build();
//
//        byte[] data = fProto.toByteArray();
//
//        ByteBuf buf = new ByteBuf(1024);
//        buf.writeByte((byte) 0x03);
//        buf.writeBytes(data);
//
//        session.send(buf.toArray());
//
//           robotInfo.doHandicarft.add(handicraftId);
//
//        } , randomTime, TimeUnit.MILLISECONDS);
//    };
//
//    public static final RobotSpinCmdAction spinCmdAction = (session, spinId) -> {
//        int randomTime = RandomUtils.randomInt(1000, 5000);
//        RobotInfo robotInfo = RobotManager.get(session);
//        RobotRun.scheduledExecutorService.schedule(() -> {
//
//            Monitor.addActionRequest(session, FProto.Action.Sewing);
//            robotInfo.requestTimeMap.put(FProto.Action.Sewing, System.currentTimeMillis());
//            LifeArgs lifeArgs = LifeArgs.newBuilder()
//                    .setId(spinId)
//                    .setLifeType(PBLifeType.Sewing)
//                    .build();
//
//            FProto fProto = FProto.newBuilder()
//                    .setKey(FProto.Key.Life)
//                    .setAction(FProto.Action.Sewing)
//                    .setLifeArgs(lifeArgs)
//                    .build();
//
//            byte[] data = fProto.toByteArray();
//
//            ByteBuf buf = new ByteBuf(1024);
//            buf.writeByte((byte) 0x03);
//            buf.writeBytes(data);
//
//            session.send(buf.toArray());
//
//            robotInfo.doSpin.add(spinId);
//
//        } , randomTime, TimeUnit.MILLISECONDS);
//    };
//
//
//
//    static {
//        lifeCookItems.put(10000 , Map.of(5011001, 1));
//        lifeCookItems.put(10001 , Map.of(5011003, 1));
//        lifeCookItems.put(10002 , Map.of(5012003, 1));
//        lifeCookItems.put(10003 , Map.of(5011001, 1));
//        lifeCookItems.put(10004 , Map.of(5011001, 1));
//        lifeCookItems.put(10005 , Map.of(5011001, 1));
//        lifeCookItems.put(10006 , Map.of(5012003, 1));
//        lifeCookItems.put(10007 , Map.of(5011011, 1));
//        lifeCookItems.put(10008 , Map.of(5011007, 1));
//        lifeCookItems.put(10009 , Map.of(5011001, 1));
//        lifeCookItems.put(10010 , Map.of(5011004, 1));
//        lifeCookItems.put(20000 , Map.of(5023041, 1 , 5011014,3 ,5012001, 4));
//        lifeCookItems.put(20001 , Map.of(5023002, 1 , 5011001,3 ));
//        lifeCookItems.put(20003 , Map.of(5023003, 1 , 5011017,5 ,5011019, 5));
//        lifeCookItems.put(20004 , Map.of(5023004, 1 , 5011020,5 ,5011015, 5));
//        lifeCookItems.put(20005 , Map.of(5023042, 2 , 5011012,3 ,5012003, 6, 5011015, 6));
//        lifeCookItems.put(20006 , Map.of(5012011, 5 , 5011001,6 ,5011018, 6));
//        lifeCookItems.put(20007 , Map.of(5023005, 1 , 5011001,6 ,5011012, 3));
//
//
//        makePropItems.put(5032001,Map.of(5031001,5));
//        makePropItems.put(5032002,Map.of(5031002,5));
//        makePropItems.put(5032003,Map.of(5031002,5));
//        makePropItems.put(5032004,Map.of(5031003,5));
//        makePropItems.put(5032005,Map.of(5031004,5));
//        makePropItems.put(5032006,Map.of(5031005,5));
//        makePropItems.put(5032007,Map.of(5031006,5));
//        makePropItems.put(5032008,Map.of(5031007,5));
//        makePropItems.put(5032009,Map.of(5031007,5));
//
//        handicraftItems.put(5052001, Map.of(5032001, 6));
//        handicraftItems.put(5052002, Map.of(5032002, 4));
//        handicraftItems.put(5052003, Map.of(5032010, 6));
//        handicraftItems.put(5052004, Map.of(5032011, 4));
//        handicraftItems.put(5052005, Map.of(5032013, 6));
//        handicraftItems.put(5052006, Map.of(5032008, 4));
//        handicraftItems.put(5052007, Map.of(5032010, 6));
//        handicraftItems.put(5052008, Map.of(5032011, 4));
//        handicraftItems.put(5052009, Map.of(5032013, 6));
//        handicraftItems.put(5052010, Map.of(5032014, 4));
//
//        handicraftItems.put(5052011, Map.of(5032001, 6));
//        handicraftItems.put(5052012, Map.of(5032002, 4));
//        handicraftItems.put(5052013, Map.of(5032004, 6));
//        handicraftItems.put(5052014, Map.of(5032005, 4));
//        handicraftItems.put(5052015, Map.of(5032007, 6));
//        handicraftItems.put(5052016, Map.of(5032008, 4));
//        handicraftItems.put(5052017, Map.of(5032010, 6));
//        handicraftItems.put(5052018, Map.of(5032011, 4));
//        handicraftItems.put(5052019, Map.of(5032013, 6));
//        handicraftItems.put(5052020, Map.of(5032014, 4));
//        handicraftItems.put(5052021, Map.of(5032007, 6));
//        handicraftItems.put(5052022, Map.of(5032008, 4));
//        handicraftItems.put(5052023, Map.of(5032010, 6));
//        handicraftItems.put(5052024, Map.of(5032011, 4));
//
//        spinItems.put(5032001, Map.of(5031001 , 3));
//        spinItems.put(5032002, Map.of(5031001 , 3));
//        spinItems.put(5032003, Map.of(5031001 , 3));
//        spinItems.put(5032004, Map.of(5031002 , 3));
//        spinItems.put(5032005, Map.of(5031002 , 3));
//        spinItems.put(5032006, Map.of(5031002 , 3));
//        spinItems.put(5032007, Map.of(5031003 , 3));
//        spinItems.put(5032008, Map.of(5031003 , 3));
//        spinItems.put(5032009, Map.of(5031003 , 3));
//        spinItems.put(5032010, Map.of(5031004 , 3));
//        spinItems.put(5032011, Map.of(5031004 , 3));
//        spinItems.put(5032012, Map.of(5031004 , 3));
//        spinItems.put(5032013, Map.of(5031002 , 5));
//        spinItems.put(5032014, Map.of(5031003 , 5));
//        spinItems.put(5032015, Map.of(5032001 , 1,5032004,1, 5032007,1 ,5032010,1));
//        spinItems.put(5032016, Map.of(5032002 , 1,5032005,1, 5032008,1 ,5032011,1));
//        spinItems.put(5032017, Map.of(5032003 , 1,5032006,1, 5032009,1 ,5032012,1));
//
//        spinItems.put(5032018, Map.of(5032013 , 5));
//        spinItems.put(5032019, Map.of(5032014 , 5));
//        spinItems.put(5032020, Map.of(5032018 , 1,5032019,1));
//
//    }
//
//
//    public static boolean startCookTask (KcpSession session) {
////        Monitor.addActionRequest(session , FProto.Action.CookingFood);
//        RobotInfo robotInfo = RobotManager.get(session);
//
//        Set<Integer> cookIds = new HashSet<>(RobotLifeAction.lifeCookItems.keySet());
//        cookIds.removeAll(robotInfo.doCooking);
//        if(CollUtil.isNotEmpty(cookIds)) {
//            int cookId = cookIds.stream().findFirst().get();
//            RobotLifeAction.cooking( session, cookId);
//            return false;
//        } else {
////            doCooking.clear();
//            return true;
////            //烹饪结束，下一个纺织
////            startHandicraftTask(session);
//        }
//    }
//
//
//
//    public static void startCookTask(KcpSession session, Map.Entry<Integer,Map<Integer,Integer>> entry) {
//        Map<Integer, Integer> costMap = entry.getValue();
////        Logger.info(RobotManager.get(session).toString() + " 开始请求烹饪Id:" + entry.getKey() + " , 需要消耗道具：" + costMap);
//        costMap.forEach((k,v) -> RobotAction.addItem(session, k , v));
//        ThreadUtils.sleep(1000L);
//        cooking(session, entry.getKey());
////        RobotAction.scheduled.schedule(() -> cooking(session, entry.getKey()),1000L, TimeUnit.MILLISECONDS);
//    }
//
//    public static void startMakeTask (KcpSession session) {
//        //增加道具
//        for (Map.Entry<Integer,Map<Integer,Integer>> entry: makePropItems.entrySet()) {
//
//            long time = RobotAction.taskTimes.getOrDefault(session ,0L);
//            RobotRun.scheduledExecutorService.schedule(() -> {
//                Map<Integer, Integer> costMap = entry.getValue();
////                Logger.info(RobotManager.get(session).toString() + " 开始请求制作Id:" + entry.getKey() + " , 需要消耗道具：" + costMap);
//                costMap.forEach((k,v) -> RobotAction.addItem(session, k , v));
//            }, time , TimeUnit.MILLISECONDS);
//
//            long startCookTime = RobotAction.taskTimes.get(session) + 1000;
//            RobotRun.scheduledExecutorService.schedule(() -> making(session,entry.getKey()), startCookTime, TimeUnit.MILLISECONDS);
//
////            ThreadUtils.sleep(1000L);
////            making(session, entry.getKey());
////            ThreadUtils.sleep(300L);
//        }
//    }
//
//    public static boolean startHandicraftTask(KcpSession session) {
//        RobotInfo robotInfo = RobotManager.get(session);
//        Set<Integer> cookIds = new HashSet<>(RobotLifeAction.handicraftItems.keySet());
//        cookIds.removeAll(robotInfo.doHandicarft);
//        if(CollUtil.isNotEmpty(cookIds)) {
//            int cookId = cookIds.stream().findFirst().get();
//            RobotLifeAction.handicraft( session, cookId);
//            return false;
//        } else {
////            doHandicarft.clear();
//            return true;
////            //制作结束，下一个纺织
////            startSpinTask(session);
//        }
//
////        //增加道具
////        for (Map.Entry<Integer,Map<Integer,Integer>> entry: handicraftItems.entrySet()) {
////            Map<Integer, Integer> costMap = entry.getValue();
////
////            costMap.forEach((k,v) -> {
////                final long time = RobotAction.taskTimes.getOrDefault(session ,0L) + 3000;
////                RobotRun.scheduledExecutorService.schedule(() -> RobotAction.addItem(session, k , v),time,TimeUnit.MILLISECONDS);
////                RobotAction.taskTimes.put(session, time);
////            });
////            final long time = RobotAction.taskTimes.getOrDefault(session ,0L) + 5000;
////            RobotRun.scheduledExecutorService.schedule(() -> handicraft(session,entry.getKey()), time, TimeUnit.MILLISECONDS);
////            RobotAction.taskTimes.put(session, time);
////        }
//    }
//
//    public static boolean startSpinTask(KcpSession session) {
//        RobotInfo robotInfo = RobotManager.get(session);
//        Set<Integer> cookIds = new HashSet<>(RobotLifeAction.spinItems.keySet());
//        cookIds.removeAll(robotInfo.doSpin);
//        if(CollUtil.isNotEmpty(cookIds)) {
//            int cookId = cookIds.stream().findFirst().get();
//            RobotLifeAction.spin( session, cookId);
//            return false;
//        } else {
////            doSpin.clear();
//            return true;
////            //制作结束，下一个烹饪
////            startCookTask(session);
//        }
//
////        //增加道具
////        for (Map.Entry<Integer,Map<Integer,Integer>> entry: spinItems.entrySet()) {
////            Map<Integer, Integer> costMap = entry.getValue();
////
////            costMap.forEach((k,v) -> {
////                final long time = RobotAction.taskTimes.getOrDefault(session ,0L) + 3000;
////                RobotRun.scheduledExecutorService.schedule(() -> RobotAction.addItem(session, k , v),time,TimeUnit.MILLISECONDS);
////                RobotAction.taskTimes.put(session, time);
////            });
////            final long time = RobotAction.taskTimes.getOrDefault(session ,0L) + 5000;
////            RobotRun.scheduledExecutorService.schedule(() -> spin(session,entry.getKey()), time, TimeUnit.MILLISECONDS);
////            RobotAction.taskTimes.put(session, time);
////        }
//    }
//
//    public static void spin(KcpSession session , int spinId ) {
//
//        RobotInfo robotInfo = RobotManager.get(session);
//        robotInfo.action = FProto.Action.Sewing;
//        Map<Integer, Integer> costMap = spinItems.get(spinId);
//        RobotSpinCmdActionWrap spinCmdActionWrap = new RobotSpinCmdActionWrap(spinId, costMap.keySet(), spinCmdAction);
////        Logger.info(RobotManager.get(session).toString() + " 开始请求纺织Id:" + spinId + " , 需要消耗道具：" + costMap);
//        robotInfo.robotSpinCmdActionWrap = spinCmdActionWrap;
//        AtomicLong time = new AtomicLong(1000);
//        costMap.forEach((k,v) -> {
//
//            RobotRun.scheduledExecutorService.schedule(() -> RobotAction.addItem(session, k , v),time.getAndAdd(1000),TimeUnit.MILLISECONDS);
//        });
//
////        RobotInfo robotInfo = RobotManager.get(session);
////        robotInfo.requestTimeMap.put(FProto.Action.FProto_Sewing, System.currentTimeMillis());
////        LifeArgs lifeArgs = LifeArgs.newBuilder()
////                .setId(spinId)
////                .setLifeType(PBLifeType.PBLifeType_Sewing)
////                .build();
////
////        FProto fProto = FProto.newBuilder()
////                .setKey(FProto.Key.Life)
////                .setAction(FProto.Action.FProto_Sewing)
////                .setLifeArgs(lifeArgs)
////                .build();
////
////        byte[] data = fProto.toByteArray();
////
////        ByteBuf buf = new ByteBuf(1024);
////        buf.writeByte((byte) 0x03);
////        buf.writeBytes(data);
////
////        session.send(buf.toArray());
//    }
//
//
//    public static void handicraft(KcpSession session , int makeId ) {
//
//        RobotInfo robotInfo = RobotManager.get(session);
//        robotInfo.action = FProto.Action.Handicraft;
//        Map<Integer, Integer> costMap = handicraftItems.get(makeId);
//        if(costMap == null) {
//            System.err.println("...");
//        }
//        RobotHandicraftCmdActionWrap handicraftCmdActionWrap = new RobotHandicraftCmdActionWrap(makeId, costMap.keySet(), handicraftCmdAction);
////        Logger.info(RobotManager.get(session).toString() + " 开始请求制作Id:" + makeId + " , 需要消耗道具：" + costMap);
//        robotInfo.handicraftCmdActionWrap = handicraftCmdActionWrap;
//        AtomicLong time = new AtomicLong(1000);
//        costMap.forEach((k,v) -> {
//            RobotRun.scheduledExecutorService.schedule(() -> RobotAction.addItem(session, k , v),time.getAndAdd(1000),TimeUnit.MILLISECONDS);
//        });
//
////        RobotInfo robotInfo = RobotManager.get(session);
////        robotInfo.requestTimeMap.put(FProto.Action.FProto_Handicraft, System.currentTimeMillis());
////        LifeArgs lifeArgs = LifeArgs.newBuilder()
////                .setId(makeId)
////                .setLifeType(PBLifeType.PBLifeType_Handicraft)
////                .build();
////
////        FProto fProto = FProto.newBuilder()
////                .setKey(FProto.Key.Life)
////                .setAction(FProto.Action.FProto_Handicraft)
////                .setLifeArgs(lifeArgs)
////                .build();
////
////        byte[] data = fProto.toByteArray();
////
////        ByteBuf buf = new ByteBuf(1024);
////        buf.writeByte((byte) 0x03);
////        buf.writeBytes(data);
////
////        session.send(buf.toArray());
//    }
//
//
//    public static void making(KcpSession session , int makeId ) {
//
//        LifeArgs lifeArgs = LifeArgs.newBuilder()
//                .setId(makeId)
//                .setLifeType(PBLifeType.Making)
//                .build();
//
//        FProto fProto = FProto.newBuilder()
//                .setKey(FProto.Key.Life)
//                .setAction(FProto.Action.MakeProp)
//                .setLifeArgs(lifeArgs)
//                .build();
//
//        byte[] data = fProto.toByteArray();
//
//        ByteBuf buf = new ByteBuf(1024);
//        buf.writeByte((byte) 0x03);
//        buf.writeBytes(data);
//
//        session.send(buf.toArray());
//    }
//
//    public static void cooking(KcpSession session , int cookId ) {
//
//        RobotInfo robotInfo = RobotManager.get(session);
//        robotInfo.action = FProto.Action.CookingFood;
//        Map<Integer, Integer> costMap = lifeCookItems.get(cookId);
//        RobotCookActionWrap robotCookActionWrap = new RobotCookActionWrap(cookId, costMap.keySet(), robotCookCmdAction);
////        Logger.info(RobotManager.get(session).toString() + " 开始请求烹饪Id:" + cookId + " , 需要消耗道具：" + costMap);
//        robotInfo.robotCookActionWrap = robotCookActionWrap;
//        AtomicLong time = new AtomicLong(1000);
//        costMap.forEach((k,v) -> {
//            int randomTime = RandomUtils.randomInt(1000, 5000);
//            RobotRun.scheduledExecutorService.schedule(() -> RobotAction.addItem(session, k , v),time.getAndAdd(randomTime),TimeUnit.MILLISECONDS);
//        });
////        final long time = RobotAction.taskTimes.getOrDefault(session ,0L) + 5000;
////        RobotRun.scheduledExecutorService.schedule(() -> {
////            robotInfo.requestTimeMap.put(FProto.Action.CookingFood, System.currentTimeMillis());
////            LifeArgs lifeArgs = LifeArgs.newBuilder()
////                    .setId(cookId)
////                    .setLifeType(PBLifeType.Cooking)
////                    .build();
////
////            FProto fProto = FProto.newBuilder()
////                    .setKey(FProto.Key.Life)
////                    .setAction(FProto.Action.CookingFood)
////                    .setLifeArgs(lifeArgs)
////                    .build();
////
////            byte[] data = fProto.toByteArray();
////
////            ByteBuf buf = new ByteBuf(1024);
////            buf.writeByte((byte) 0x03);
////            buf.writeBytes(data);
////
////            session.send(buf.toArray());
////        } , time, TimeUnit.MILLISECONDS);
////        RobotAction.taskTimes.put(session, time);
////        RobotLifeAction.doCooking.add(cookId);
//
//
//    }
//
//
//}
